<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/11/17
 * Time: 10:45
 */

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\wc\DoctorModel;
use bc\model\wc\HospitalModel;
use bc\model\wc\BookingModel;
use bc\model\UserModel;
use bc\model\wc\SpreadModel;
use bc\util\backend\Page;

class DoctorController extends BackendController
{
    public function actionIndex()
    {
        $where = array (
            'bc_wc_doctor.is_invalid' => 1
        );

        $page = $this->get('page', 1);
        $sort = 'id desc';
        $doctorM = new DoctorModel ();
        $doctorM->columns='bc_wc_doctor.id as d_id,user_id,hospital_id,phone,bc_wc_hospital.id as h_id,name,address,bc_user.id as uid,real_name,nick_name,wxid';
        $doctorList = $doctorM->getDoctorList ($where,$page,self::PAGE_SIZE,$sort);
        $total = $doctorM->getTotal ( $where );
        $pagination = new Page ( $total, self::PAGE_SIZE );

        $row['pagination'] = $pagination;
        $row ['doctorList'] = $doctorList;
        return $this->render('index',$row);
    }

    /*
     * 编辑
     * */
    public function actionEdit()
    {
        $id = $this->get('id');
        $where=array('bc_wc_doctor.id'=>$id);
        $doctorM = new DoctorModel();
        $doctorM->columns='bc_wc_doctor.id as d_id,user_id,hospital_id,phone,bc_wc_hospital.id as h_id,name,address,bc_user.id as uid,real_name,nick_name,wxid';
        $doctorList = $doctorM->getDoctorList ($where);
        $hospitalM = new HospitalModel();
        $hospitalList=$hospitalM->getList(array('is_invalid'=>1));
        $data['row']= $doctorList[0];
        $data['hospitalList']= $hospitalList;
        return $this->render('edit',$data);

    }

    /*
 * 保存
 * */
    public function actionSave()
    {
        $id = $this->post('id');
        $hospital_id = $this->post('h_id');
        $phone = $this->post('phone');
        $name = $this->post('name');
        $user_id = $this->post('uid');
//        $wx = $this->get('wx');
//        $openid = $this->get('openid');
        $data = array();
        $data['user_id'] = $user_id;
        $data['hospital_id'] = $hospital_id;
        $data['phone'] = $phone;

        $model = new DoctorModel();

        if(empty($id))
        {
            $data['create_time']=time();
            $data['update_time']=time();
            $data['is_invalid']=1;
            $model->add($data);
        }else {

            $data['update_time']=time();
            $data['hospital_id'] = $this->post('hospital_id');
            $model->modify($data, $id);

            //修改医生的真实姓名
            $Udata = array();
            $Udata['real_name'] = $name;
            $Udata['mobile'] = $phone;

            $usermoble = new UserModel();
            $usermoble->modify($Udata, $user_id);

            //修改所属医院
        }
        $this->redirect('/doctor/index');
    }

    /*
     * 删除
     * */
    public function actionDelet()
    {
        $id = $this->get('id');
        $model = new DoctorModel();
        $data['update_time']=time();
        $data['is_invalid']=0;
        $model->modify($data, $id);
        $this->redirect('/doctor/index');
    }

    /*
     * 添加
     * */
    public function actionAdd()
    {
        $where=array('is_invalid' => 1);
        $hospitalM = new HospitalModel();
        $hospitalM->columns = 'id,name,address,create_time';
        $hospitalM->sort = 'id desc';
        $hospitalList = $hospitalM->getList( $where);
        $row ['hospitalList'] = $hospitalList;
        return $this->render('add',$row);
    }

    /*
     * 查看我的推荐明细
     * */
    public function actionRecommend()
    {
        $id = $this->get('id');
        $where = array(
            'doctor_id' => $id
        );
        $model = new BookingModel();
        $page = $this->get('page', 1);
        $bookingList = $model->getList( $where,$page,self::PAGE_SIZE);
        $total = $model->getTotal($where);
        $pagination = new Page($total, self::PAGE_SIZE);
        $row['pagination'] = $pagination;
        $row ['bookingList'] = $bookingList;
        return $this->render('recommend',$row);
    }

    /*
     * 我的二维码
     * */
    public function actionQr()
    {
        $id = $this->get('id');
        $where = array(
            'id' => $id
        );

        $doctorModel = new DoctorModel();
        $doctorRow = $doctorModel->getRow($where);

        $userModel = new UserModel();
        $userRow=$userModel->getRow(array('id'=>$doctorRow['user_id']));
        $row['doctorRow'] = $doctorRow;
        $row['userRow'] = $userRow;
        return $this->render('qrcode',$row);
    }

    /*
     * 医生查看我的推广
     * */
    public function actionSpreadlist()
    {
        $wx_name = $this->get('wx_name', '');
        if($wx_name != '')
        {
            $params = "`nickname` LIKE '%{$wx_name}%'";
        }else{
            $params = '1 = 1 ';
        }

        $sort_time = $this->get('sort_time', '');
        if($sort_time == 'desc'){
            $sort = ' create_time desc';
        }elseif($sort_time == 'asc'){
            $sort = 'create_time asc';
        }else{
            $sort = 'create_time desc';
        }

        $start_time_str = $this->get('start_time', '');
        $end_time_str = $this->get('end_time', '');
        $start_time = $start_time_str ? strtotime($start_time_str) : '';
        $end_time = $end_time_str ? strtotime($end_time_str) : '';
        if($start_time != '')
        {
            $params = $params . " and `create_time` >= '{$start_time}'";
        }

        if($end_time != '')
        {
            $end_time+=60*60*24;
            $params = $params . " and `create_time` <= '{$end_time}'";
        }

        $id = $this->get('id');
        $tag = $this->get('tag',4);
        $page = $this->get('page', 1);
        $pagesize=self::PAGE_SIZE;
        $spreadModel = new SpreadModel();
        $row=$spreadModel->getSpreadListFromDoctorID($params,$sort,$id,$tag,$page,$pagesize);

        $view=$row;
        $view['id'] = $id;
        $view['tag'] = $tag;
        $view['wx_name'] = $wx_name;
        $view['start_time'] = $start_time_str;
        $view['end_time'] = $end_time_str;
        return $this->render('spreadlist',$view);
    }
}